﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using XMK.Tadong.Model;
using XMK.Framework.DataAccess;
using XMK.Framework.Model;
using XMK.Utils;

namespace XMK.Tadong.Data
{
    public class XMKLogData
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;
        public XMKLogData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="info"></param>
        public void WriteLog(XMKLogInfo info)
        {
            string sql = @"insert into TB_XMK_LOG(user_id, oper_type, data_id, keyw, log_desc, ip) 
					       VALUES(:p_user_id,:p_oper_type,:p_data_id,:p_keyw,:p_log_desc, :p_ip)";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider,
                        "user_id", "oper_type", "data_id", "keyw", "log_desc", "ip").ToArray();
                });
            //参数设值
            cmd.SetParamValues(info.UserId,
                info.OperateType, info.DataId, info.Keyword, info.LogDesc, info.IP);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DbManager.ExecNonQuery(cmd);
        }

        /// <summary>
        /// 日志查询
        /// </summary>
        /// <param name="pageInfo">分页信息</param>
        /// <param name="operateType">操作类型</param>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">截止日期</param>
        /// <param name="userId">用户ID</param>
        /// <param name="keyw">关键词</param>
        /// <returns></returns>
        public List<XMKLogInfo> QueryLog(PageInfo pageInfo, LogOperateType operateType, 
            string userId, DateTime startDate, DateTime endDate, string keyw)
        {
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "sp_page_querylog",
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, true,
                        "oper_type", "start_date", "end_date", "user_id", "keyw").ToArray();
                });
            //参数设值
            OrderInfo order = new OrderInfo();
            order.ColName = "oper_time";
            order.Sort = "desc";
            cmd.SetParamValues(order, pageInfo,(int)operateType, startDate.ToString("yyyy-MM-dd"), 
                endDate.ToString("yyyy-MM-dd"), userId, keyw);
            cmd.DbCmdInfo.CommandType = CommandType.StoredProcedure;
            List<XMKLogInfo> logList = DbManager.GetPageInfos<XMKLogInfo>(cmd, pageInfo,
                delegate(out XMKLogInfo logModel, DataRow row)
                {
                    logModel = new XMKLogInfo
                    {
                        LogID = ParseHelper.Parse<long>(row["log_id"]),
                        UserId = ParseHelper.Parse<string>(row["user_id"]),
                        OperateTime = ParseHelper.Parse<DateTime>(row["oper_time"]),
                        //OperateType = ParseHelper.Parse<LogOperateType>(row["oper_type"]),
                        DataId = ParseHelper.Parse<long>(row["data_id"]),
                        Keyword = ParseHelper.Parse<string>(row["keyw"]),
                        LogDesc = ParseHelper.Parse<string>(row["log_desc"]),
                        IP = ParseHelper.Parse<string>(row["ip"]),
                        UserName = ParseHelper.Parse<string>(row["user_name"]),
                        UserAccount = ParseHelper.Parse<string>(row["user_account"])
                    };
                });

            return logList;
        }

        /// <summary>
        /// 日志查询
        /// </summary>
        /// <param name="operateType">操作类型</param>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">截止日期</param>
        /// <param name="keyw">关键词</param>
        /// <returns></returns>
        public DataTable QueryLog(LogOperateType operateType, string userId,
            DateTime startDate, DateTime endDate, string keyw)
        {
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "sp_querylog",
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider,
                        "oper_type", "start_date", "end_date", "user_id", "keyw").ToArray();
                });
            //参数设值
            cmd.SetParamValues((int)operateType, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), userId, keyw);
            
            cmd.DbCmdInfo.CommandType = CommandType.StoredProcedure;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="operateType">操作类型</param>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">起始日期</param>
        /// <param name="endDate">截止日期</param>
        /// <param name="keyw">关键词</param>
        /// <returns></returns>
        public DataTable DeleteLog(LogOperateType operateType, string userId,
            DateTime startDate, DateTime endDate, string keyw)
        {
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "sp_deletelog",
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider,
                        "oper_type", "start_date", "end_date", "user_id", "keyw").ToArray();
                });
            //参数设值
            cmd.SetParamValues((int)operateType, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), userId, keyw);

            cmd.DbCmdInfo.CommandType = CommandType.StoredProcedure;
            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }
    }
}
